﻿#include <iostream>
#include "list"
using namespace std;
class obverver {
public:
    obverver() {}
    virtual ~obverver(){}
    virtual void print() { cout << "obverver::print()" << endl; }
};
class A :public obverver {
public:
    A() {}
    virtual ~A() {}
    virtual void print() { cout << "A::print()" << endl; }
};
class B :public obverver {
public:
    B() {}
    virtual ~B() {}
    virtual void print() { cout << "B::print()" << endl; }
};
class C :public obverver {
public:
    C() {}
    virtual ~C() {}
    virtual void print() { cout << "C::print()" << endl; }
};
class center
{
public:
    center() {}
    virtual~center() {}
    void attach(obverver* ob) {
        bool exist = false;
        for(const auto& o:obers)
        {
            if (o == ob) {
                exist = true;
                return;
            }
        }
        if (!exist) 
        {
            obers.push_back(ob);
        }
    }
    //解绑观察者
    //在队列中查找需要解绑的观察者是否存在
    //存在则解绑，在队列中删除
    void detach(obverver* ob) {
        bool exist = false;
        for (const auto& o : obers)
        {
            if (o == ob) {
                obers.remove(ob);
                return;
            }
        }
    }

    void notify() {
        for (const auto& o : obers) {
            o->print();
        }
    
    }
private:
    list<obverver*> obers;
};
int main()
{
    center cen;
    obverver* a = new A;
    obverver* b = new B;
    obverver* c = new C;
    cen.attach(a);
    cen.attach(b);
    cen.attach(c);
    cen.notify();

    delete(a);
    a = nullptr;

    delete(b);
    b = nullptr;  
    
    delete(c);
    c = nullptr;

    return 0;
}


